package com.android.music.prolyric.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter 
{
	private static final String KEY_ID = "id";
	private static final String KEY_SONGNAME = "songname";
	private static final String KEY_SINGER = "singer";
	private static final String KEY_LYRICEN = "lyricEn";
	private static final String KEY_LYRICVI = "lyricVi";	 
	
	private static final String TAG ="DBAdapter";
	
	private static final String DATABASE_NAME = "ProLyric";
	private static final String DATABASE_TABLE ="lyrics";
	private static final int DATABASE_VERSION =1;
	
	private static final String DATABASE_CREATE = "create table lyrics(id integer primary key autoincrement, " + "songname text not null ,singer text not null,lyricEn text not null,lyricVi text not null);";
	
	private final Context conext;
	
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;
	
	public DBAdapter(Context ctx)
	{
		this.conext =ctx;
		DBHelper = new DatabaseHelper(conext);
	}
	private static class DatabaseHelper extends SQLiteOpenHelper
	{

		public DatabaseHelper(Context context) 
		{
			super(context,DATABASE_NAME,null,DATABASE_VERSION);
		}
		@Override
		public void onCreate(SQLiteDatabase db)
		{
			try
			
			{
				db.execSQL(DATABASE_CREATE);
			}
			catch(SQLException e)
			{
				e.printStackTrace();
			}
		}
		@Override
		public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + "'which will destroy all old data");
			db.execSQL("DROP TABLE IF EXITS lyrics");
			onCreate(db);
		}
		
	}
	//open the database
	public DBAdapter open() throws SQLException
	{
		db =DBHelper.getWritableDatabase();
		return this;
	}
	//close the database
	public void close()
	{
		DBHelper.close();
	}
	//insert a lyrics into the database
	public long insertLyrics(String songName,String singer,String lyrEn,String lyrVi)
	{
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_SONGNAME, songName);
		initialValues.put(KEY_SINGER, singer);
		initialValues.put(KEY_LYRICEN, lyrEn);
		initialValues.put(KEY_LYRICVI, lyrVi);
		return db.insert(DATABASE_TABLE, null, initialValues);
		
	}
	
	//retrieves a particular contacts
	public Cursor getLyricById(long rowid) throws SQLException
	{
		Cursor mCusor = db.query(true, DATABASE_TABLE,null, KEY_ID + "=" + rowid, null, null, null, null, null);
		if(mCusor!=null)
			mCusor.moveToFirst();
		
		return mCusor;
	}
	
	public Cursor getAllLyrics() throws SQLException
	{
		Cursor mCusor = db.query(true, DATABASE_TABLE,null,null, null, null, null, null, null);
		if(mCusor!=null)
			mCusor.moveToFirst();
		
		return mCusor;
	}
	
	public Cursor getLyricByName(String songname, String singer) throws SQLException
	{
		Cursor mCusor = db.query(true, DATABASE_TABLE,null, KEY_SONGNAME + " LIKE '" + songname + "' AND " + KEY_SINGER + " LIKE '" + singer + "'", null, null, null, null, null);
		if(mCusor!=null)
			mCusor.moveToFirst();
		
		return mCusor;
	}
	
	//update a contact
	public boolean updateLyrics(String nameSong,String singer,String lyrEn,String lyrVi)
	{
		ContentValues args = new ContentValues();
		args.put(KEY_LYRICEN,lyrEn);
		args.put(KEY_LYRICVI,lyrVi);
		return db.update(DATABASE_TABLE, args, KEY_SONGNAME + " LIKE '" + nameSong + "' AND " + KEY_SINGER + " LIKE '" + singer + "'", null)>0;
	}
}

